package com.xhwy.service.impl;

import com.xhwy.entity.ProductType;
import com.xhwy.mapper.ProductTypeMapper;
import com.xhwy.service.ProductTypeService;
import com.xhwy.util.PageUtil;
import com.xhwy.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

//业务层
@Service
public class ProductTypeServiceImpl implements ProductTypeService {
    @Autowired
    private ProductTypeMapper productTypeMapper;
    @Override
    public PageUtil<ProductType> queryAllByLimit(ProductType productType, PageUtil<ProductType> pageUtil) {
        List<ProductType> productTypes = productTypeMapper.queryAll(productType, pageUtil);
        int count = productTypeMapper.count(productType);
        pageUtil.setCode("001");
        pageUtil.setList(productTypes);
        pageUtil.setTotalRecords(count);
        return pageUtil;
    }

    @Override
    public R addProductType(ProductType productType) {
        if(productType == null) return R.fail("数据异常");
        //设置创建时间
        productType.setCreateTime(new Timestamp(new Date().getTime()));
        int i = productTypeMapper.addProductType(productType);
        if(i > 0) return R.ok("添加成功");
        return R.fail("添加失败");
    }

    @Override
    public R updateProductType(ProductType productType) {
        if(productType == null) return R.fail("数据异常");
        int i = productTypeMapper.updateProductType(productType);
        if(i > 0) return R.ok("修改成功");
        return R.fail("修改失败");
    }

    @Override
    public R deleteProductTypeByProductTypeId(Integer productTypeId) {
        if(productTypeId == null) return R.fail("数据异常");
        int i = productTypeMapper.deleteProductTypeByProductTypeId(productTypeId);
        if(i > 0) return R.ok("删除成功");
        return R.fail("删除失败");
    }

    /**
     * 查询类别数据集合
     *
     * @return
     */
    @Override
    public R list() {
        List<ProductType> list = productTypeMapper.queryAll(null, null);
        return R.ok("查询成功", list);
    }
}
